package com.cqut.forum.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cqut.forum.entity.User;
import com.cqut.forum.service.UserService;

/**
 * 用户退出controller
 * @author yangxun
 */
@Controller
public class UserBasicAction {
	
	@Resource
	private UserService userService;
	@RequestMapping("/user/user_basic")
	public String userBasic(HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		if(user == null){
			return "/error.html";
		}
		return "/pages/user/user_control_main.html";
	}
	
	@SuppressWarnings("deprecation")
	@RequestMapping("/user/basic_info")
	@ResponseBody
	public Map<String, Object> userBasicInfo(HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		Map<String, Object> reuslt = new HashMap<String, Object>();
		if(user!=null){
			reuslt.put("userPhoto", user.getUserPhoto());
			reuslt.put("userName", user.getUserName());
			reuslt.put("userEmail",user.getUserEmail());
			reuslt.put("userQQ", user.getUserQQ());
			reuslt.put("userSex", user.getUserSex());
			if(user.getUserBirthday()!=null)
				reuslt.put("userBirthday", ((Date)user.getUserBirthday()).toLocaleString());
			else
				reuslt.put("userBirthday", "");
			reuslt.put("userPlace", user.getUserPlace());
			reuslt.put("userWork", user.getUserWork());
			reuslt.put("isLogin", true);
			reuslt.put("userScore", user.getUserScore());
		}
		else{
			reuslt.put("isLogin", false);
		}
		return reuslt;
	}
	
	@RequestMapping("/user/change_password")
	public String updateUser(HttpServletRequest request,String password,String oldPassword){
		User user = (User)request.getSession().getAttribute("user");
		if(!oldPassword.equals(user.getUserPassword())){
			return "/pages/user/error.html";
		}
		user.setUserPassword(password);
		user = userService.update(user);
		request.getSession().setAttribute("user", user);
		
		return "/pages/user/success.html";
	}
	
	@RequestMapping("/user/update_user")
	public String updateUser (HttpServletRequest request,String userEmail,
			String userQQ,String userSex,Date userBirthday,String userPlace,String userWork){
		User old = (User)request.getSession().getAttribute("user");
		old.setUserEmail(userEmail);
		old.setUserQQ(userQQ);
		old.setUserSex(userSex);
		old.setUserBirthday(userBirthday);
		old.setUserPlace(userPlace);
		old.setUserWork(userWork);
		
		userService.update(old);
		request.getSession().setAttribute("user", old);
		return "/pages/user/user_control_basic.html";
	}
	
	@RequestMapping("/user/check_password")
	@ResponseBody
	public String checkPassword(HttpServletRequest request,String oldPassword){
		User user = (User)request.getSession().getAttribute("user");
		if(user.getUserPassword().equals(oldPassword)){
			return "true";
		}
		return "false";
	}

	@RequestMapping("/user/dongtai_control")
	@ResponseBody
	public List<Map<String, Object>> controlDongtai(HttpServletRequest request){
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		
		
		return result;
	}

}
